package com.pureut.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.equipment.domain.InspectionItem;
import com.pureut.equipment.domain.dto.InspectionItemDto;
import com.pureut.equipment.domain.dto.InspectionItemPlanDto;
import com.pureut.equipment.domain.vo.InspectionItemVo;
import com.pureut.equipment.mapper.InspectionItemMapper;
import com.pureut.equipment.mapper.InspectionItemPlanMapper;
import com.pureut.equipment.service.InspectionItemService;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 *
 * Author:点验项目实现层
 * Date:2023/04/24 15:19
 * @DESC
 */
@Service
public class InspectionItemServiceImpl extends ServiceImpl<InspectionItemMapper, InspectionItem> implements InspectionItemService {
    /**
     * 点验项目mapper
     */
    @Resource
    InspectionItemMapper inspectionItemMapper;

    /**
     * 基础模块fegin
     */
    @Resource
    FeignService feignService;

    /**
     * 点检计划mapper
     */
    @Resource
    InspectionItemPlanMapper inspectionItemPlanMapper;

    /**
     * 点验项目列表
     *
     * @param inspectionItemVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "inspection_item")
    public List<InspectionItemDto> getList(InspectionItemVo inspectionItemVo) {
        //获取生成方式字典
        List<SysDictData> inspectionTypeArray = DictUtils.getDictCache("inspection_item_inspection_type");
        Map<String, String> inspectionTypeMap = inspectionTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //获取点检方式字典
        List<SysDictData> modeArray = DictUtils.getDictCache("inspection_item_point_test_mode");
        Map<String, String> modeMap = modeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        List<InspectionItemDto> list = inspectionItemMapper.getList(inspectionItemVo);
        for (InspectionItemDto entity : list) {
            entity.setInspectionTypeDict(inspectionTypeMap.get(entity.getInspectionType()));
            entity.setPointTestModeDict(modeMap.get(entity.getPointTestMode()));
        }
        return list;
    }

    /**
     * 新增
     *
     * @param inspectionItemVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(InspectionItemVo inspectionItemVo) throws Exception {

        InspectionItem inspectionItem = new InspectionItem();
        String authorityCoding = feignService.getAuthorityCoding("device:spotCheck:project:list");
        if (authorityCoding == null) {
            throw new GlobalException("该单据未进行配置，无法生成单据编码");
        }
        inspectionItem.setPointTestProjectCode(authorityCoding)
                .setPointTestProjectName(inspectionItemVo.getPointTestProjectName())
                .setPointTestPosition(inspectionItemVo.getPointTestPosition())
                .setPointTestMode(inspectionItemVo.getPointTestMode())
                .setWorkingHour(inspectionItemVo.getWorkingHour())
                .setPointTestContent(inspectionItemVo.getPointTestContent())
                .setInspectionStandard(inspectionItemVo.getInspectionStandard())
                .setInspectionType(inspectionItemVo.getInspectionType())
                .setDecisionResult(inspectionItemVo.getDecisionResult())
                .setDeptId(SecurityUtils.getDeptId())
                .setCreateBy(SecurityUtils.getUsername())
                .setCreateTime(new Date())
                .setRemark(inspectionItemVo.getRemark());

        return save(inspectionItem);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteInspectionItem(Long id) {
        InspectionItem inspectionItem = getById(id);
        //TODO 该项目已关联点检计划，无法删除
        List<InspectionItemPlanDto> inspectionItemPlanDtos = inspectionItemPlanMapper.selectInspectionItemPlan(id);
        if(inspectionItemPlanDtos.size() > 0){
            throw new GlobalException("该项目已关联点检计划，无法删除");
        }
        return removeById(id);
    }

    /**
     * 修改
     * @param inspectionItemVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateInspectionItem(InspectionItemVo inspectionItemVo) {
        InspectionItem byId = getById(inspectionItemVo.getId());
        byId.setPointTestProjectName(inspectionItemVo.getPointTestProjectName())
                .setPointTestPosition(inspectionItemVo.getPointTestPosition())
                .setPointTestMode(inspectionItemVo.getPointTestMode())
                .setWorkingHour(inspectionItemVo.getWorkingHour())
                .setPointTestContent(inspectionItemVo.getPointTestContent())
                .setInspectionStandard(inspectionItemVo.getInspectionStandard())
                .setInspectionType(inspectionItemVo.getInspectionType())
                .setDecisionResult(inspectionItemVo.getDecisionResult())
                .setRemark(inspectionItemVo.getRemark());
        return updateById(byId);
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @Override
    public InspectionItemDto queryInspectionItem(Long id) {
        InspectionItemDto dataById = inspectionItemMapper.getDataById(id);

        //获取生成方式字典
        List<SysDictData> inspectionTypeArray = DictUtils.getDictCache("inspection_item_inspection_type");
        Map<String, String> inspectionTypeMap = inspectionTypeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //获取点检方式字典
        List<SysDictData> modeArray = DictUtils.getDictCache("inspection_item_point_test_mode");
        Map<String, String> modeMap = modeArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        dataById.setInspectionTypeDict(inspectionTypeMap.get(dataById.getInspectionType()));
        dataById.setPointTestModeDict(modeMap.get(dataById.getPointTestMode()));

        return dataById;
    }
}
